package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyResCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemSecretResBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.listener.OnSecondClickListener;
import com.chuanjiabao.wisdom.util.GlideUtil;
import com.chuanjiabao.wisdom.util.LogUtil;
import com.chuanjiabao.wisdom.util.TimeUtil;

import java.util.ArrayList;
import java.util.List;

public class SecretResAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<FamilyResCallbackBean.DataBeanX.DataBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;
    boolean isShow;
    public List<CheckBox> checkBoxes = new ArrayList<>();
    public OnSecondClickListener onSecondClickListener;

    public void setOnSecondClickListener(OnSecondClickListener onSecondClickListener) {
        this.onSecondClickListener = onSecondClickListener;
    }

    public SecretResAdapter(Context context, List<FamilyResCallbackBean.DataBeanX.DataBean> strings,
                            boolean isShow) {
        this.mcontext = context;
        this.strings = strings;
        this.isShow = isShow;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemSecretResBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_secret_res, parent, false);
        binding.setAdapter(this);
        checkBoxes.add(binding.tvModuleSelect);
        return new TourViewHolder(binding);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;
        if (4 == strings.get(position).getFile_type()) {
            tv.tvModuleHint.setVisibility(View.GONE);
            tv.rlModulePw.setVisibility(View.VISIBLE);
            WebSettings settings = tv.pwModuleView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setSupportZoom(false);
            tv.pwModuleView.setBackgroundColor(mcontext.getColor(R.color.transparent));
            tv.pwModuleView.loadDataWithBaseURL("", strings.get(position).getContent(),
                    "text/html", "UTF-8", null);
        } else if (1 == strings.get(position).getFile_type()) {
            tv.tvModuleHint.setVisibility(View.GONE);
            tv.ivModule.setVisibility(View.VISIBLE);

            GlideUtil.setUrlImg(
                    mcontext,
                    strings.get(position).getThum(),
                    strings.get(position).getFile(),
                    tv.ivModule
            );
        } else if (2 == strings.get(position).getFile_type()) {
            tv.tvModuleHint.setVisibility(View.VISIBLE);
            tv.ivModulePlay.setVisibility(View.VISIBLE);
            tv.ivModule.setVisibility(View.VISIBLE);

            GlideUtil.setUrlImg(
                    mcontext,
                    strings.get(position).getThum(),
                    strings.get(position).getFile(),
                    tv.ivModule
            );

            String videoInfo = strings.get(position).getVideo_info();
            if (!TextUtils.isEmpty(videoInfo)) {
                double value = Double.parseDouble(videoInfo);
                tv.tvModuleHint.setText(TimeUtil.formatTime(Double.valueOf(value).longValue()));
            } else tv.tvModuleHint.setText("");
        }

        tv.tvModuleSelect.setOnClickListener(v -> {
            if (onSecondClickListener != null) {
                strings.get(position).setChecked(!strings.get(position).isChecked());
                onSecondClickListener.setOnSecondClickListener(
                        strings.get(position).getId(),
                        strings.get(position).getFile_type(),
                        strings.get(position).getThumVideo()
                );
            }
        });

        tv.tvModuleSelect.setChecked(strings.get(position).isChecked());
        tv.tvModuleSelect.setVisibility(isShow ? View.VISIBLE : View.GONE);

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemClick(holder.itemView, pos);
            });

            holder.itemView.setOnLongClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                return false;
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<FamilyResCallbackBean.DataBeanX.DataBean> {
        ItemSecretResBinding functionBinding;
        ImageView ivModule, ivModulePlay;
        CheckBox tvModuleSelect;
        WebView pwModuleView;
        View ivModuleToNext;
        RelativeLayout rlModulePw;
        TextView tvModuleHint;

        public TourViewHolder(ItemSecretResBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModule = dataBinding.ivModule;
            tvModuleSelect = dataBinding.tvModuleSelect;
            ivModulePlay = dataBinding.ivModulePlay;
            pwModuleView = dataBinding.pwModuleView;
            rlModulePw = dataBinding.rlModulePw;
            ivModuleToNext = dataBinding.ivModuleToNext;
            tvModuleHint = dataBinding.tvModuleHint;
        }

        @Override
        public void bindData(FamilyResCallbackBean.DataBeanX.DataBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    //全选
    public void setCheckTextSelectAllYes() {
        for (CheckBox checkBox : checkBoxes) {
            checkBox.setVisibility(View.VISIBLE);
        }
    }

    //全不选
    public void setCheckTextSelectAllNo() {
        for (CheckBox checkBox : checkBoxes) {
            checkBox.setChecked(false);
            checkBox.setVisibility(View.GONE);
        }
    }

    //刷新数据
    public void setChangeItem(int pos) {
        LogUtil.e("调用更新位置-->" + pos);
        notifyItemRemoved(pos);
    }
}